package com.sinoframework.web.servlet.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;

import com.alibaba.cloud.sentinel.rest.SentinelClientHttpResponse;
import com.alibaba.csp.sentinel.slots.block.BlockException;

public class ExceptionUtil {
	
	private static final Logger log = LoggerFactory.getLogger(ExceptionUtil.class);
	
	
	
    private ExceptionUtil() {
		 throw new IllegalStateException("ExceptionUtil Utility class");
	}

	/**
     * 	限流后处理方法
     * @param request
     * @param body
     * @param execution
     * @param ex
     * @return
     */
    public static SentinelClientHttpResponse handleException(HttpRequest request,
                                                             byte[] body, ClientHttpRequestExecution execution, BlockException ex) {
    	log.error("block: {}" , ex.getClass().getCanonicalName());
        return new SentinelClientHttpResponse("自定义限流");
    }

    /**
     * 	熔断后处理的方法
     * @param request
     * @param body
     * @param execution
     * @param ex
     * @return
     */
    public static SentinelClientHttpResponse fallback(HttpRequest request,
                                                      byte[] body, ClientHttpRequestExecution execution, BlockException ex) {
    	log.error("fallback: {}" , ex.getClass().getCanonicalName());
        return new SentinelClientHttpResponse("自定义熔断");
    }
}